package org.listener.socket;

import org.mg.engine.InstructAbstract;
import org.mg.engine.core.Session;
import org.msg.commons.Logger;

import java.io.IOException;

/**
 * Create by bo.chen in 2018/10/13 下午5:14
 */
public class HeartBeat {

    private final static String HEART_BEAT = "d799cfe251b6c4afa6b7b811a6e8ecb6f75efd35";

    public static void heartBeat() {
        new Thread(() -> {
            while(true) {
                try {
                    Thread.sleep(5000);
                    if (null == InstructAbstract.multimap || InstructAbstract.multimap.size() == 0) {
                        continue;
                    }
                    InstructAbstract.multimap.keySet().forEach(session -> {
                        Logger.log("heart beat " + session.getSocket().getInetAddress().toString());
                        try {
                            if(session.isConnect()) {
                                return;
                            }
                            distroy(session);
                        } catch (Exception e) {}
                    });
                } catch (InterruptedException e) {
                    Logger.log("heart beet failed");
                }
            }
        }).start();
    }

    public static void distroy(Session session) {
        if(null == InstructAbstract.multimap.get(session)) {
            return;
        }
        InstructAbstract.multimap.get(session).forEach(v -> {
            System.out.println(v);
        });
    }

}